package cn.lf.android.lawfirm.undertake.approval.controler;


import android.content.Context;

import com.alibaba.fastjson.JSON;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import cn.lf.android.lawfirm.AppConfig;
import cn.lf.android.lawfirm.BaseController;
import cn.lf.android.lawfirm.http.manager.EBCallback;
import cn.lf.android.lawfirm.http.manager.MyOkHttpClient;
import cn.lf.android.lawfirm.undertake.approval.modle.CaseDetail;
import cn.lf.android.lawfirm.utils.DebugLog;
import cn.lf.android.lawfirm.utils.SPUtil;

public class CaseDetailController extends BaseController {
    CaseDetailCallback mCaseDetailCallback;
    private Context context;

    public CaseDetailController(CaseDetailCallback callback, Context context) {
        this.mCaseDetailCallback = callback;
        this.context = context;
    }

    /**
     * @param caseId
     */
    public void onCaseDetail(String caseId) {
        showProgressDia(context);
        Map<String, String> params = new HashMap<String, String>();
        SPUtil spUtil = new SPUtil(context);
        params.put("caseId", caseId);
        params.put("Authorization", spUtil.getAccessToken());
        DebugLog.e("caseId=666=" + caseId);
//        DebugLog.e("Authorization=666=" + spUtil.getAccessToken());
        new MyOkHttpClient().doGet(AppConfig.LSCaseApproveRecordList_undertake_CaseInfo, params,
                new EBCallback() {
                    @Override
                    public void onEBResponse(String response) {
                        DebugLog.e("案件详情==" + response);
                        JSONObject myJsonObject;
                        JSONObject responsObject;
                        String arraylist;
                        Boolean isSuc;
                        try {
                            myJsonObject = new JSONObject(response);
                            isSuc = myJsonObject.getBoolean("isSuc");
                            if (isSuc) {
                                responsObject = myJsonObject.getJSONObject("responsObject");
                                CaseDetail bean = JSON.parseObject(responsObject.toString(), CaseDetail.class);
                                mCaseDetailCallback.onCaseDetailuccessed(bean);
                            } else {
                                mCaseDetailCallback
                                        .onCaseDetailFailed();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        dismissProgressDia();
                    }

                    @Override
                    public void onEBError(String errorMsg) {
                        mCaseDetailCallback.onCaseDetailFailed();
                        dismissProgressDia();
                    }
                }, "Authorization", spUtil.getAccessToken());
    }

    /**
     * 承办审批案件详情回调接口
     */
    public interface CaseDetailCallback {
        /**
         * 承办审批案件详情请求成功
         */
        public void onCaseDetailuccessed(CaseDetail beans);

        /**
         * 承办审批案件详情请求失败
         */
        public void onCaseDetailFailed();

    }
}
